/*
 * @Author: silence
 * @Date: 2023-08-18 10:21:26
 * @LastEditors: Please set LastEditors
 * @LastEditTime: 2023-09-27 17:04:14
 * @Description: 配网第四步：进行配网以及进度展示
 */
import 'package:ems_neutral_app/pages/DeviceNetworking/DNSetResultPage.dart';
import 'package:ems_neutral_app/pages/Mine/Language/SilLocalizations/SilLocalizations.dart';
import 'package:ems_neutral_app/utils/router/ARouter.dart';
import 'package:ems_neutral_app/utils/toast/SilToastExtends.dart';
import 'package:flutter/material.dart';
import 'package:get/get.dart';
import 'package:sil_base_widget_package/sil_base_widget_package.dart';

import '../../base/SilScaffold.dart';
import '../../utils/color/ColorUtils.dart';
import 'Controller/DeviceNetworkingController.dart';
import 'View/DNNetProgressItem.dart';

class DNSetProgressPage extends StatefulWidget {
  const DNSetProgressPage({super.key});

  @override
  State<DNSetProgressPage> createState() => _DNSetProgressPageState();
}

class _DNSetProgressPageState extends State<DNSetProgressPage> {
  DeviceNetworkingController _controller =
      Get.put(DeviceNetworkingController());
  bool _isDeviceNetworking = true;

  @override
  void initState() {
    /// 模拟配网进度
    _controller.beginDeviceNetworking(context).then((isSuccess) {
      _isDeviceNetworking = false;

      /// 延迟，让用户看到状态后，在跳转
      Future.delayed(Duration(milliseconds: 800), () {
        ARoute.gotoPage(context, DNSetResultPage(isSuccess: isSuccess));

        /// 初始化配网状态
        _controller.initDeviceNetProgressState();
      });
    });
    super.initState();
  }

  Widget build(BuildContext context) {
    return SilScaffold(
        title: LangOf(context)?.deviceNetworking?.connectingDevice, //'正在连接设备',
        backButtonFunc: () {
          if (_isDeviceNetworking == true) {
            // SilToast.show('设备正在配网中，请稍后...');
            SilToastExtends.toastMsg(
                LangOf(context)?.deviceNetworking?.connectingDevicePrompt);
          } else {
            // 如果可以返回，就返回呗
            if (Navigator.canPop(context)) Navigator.pop(context);
          }
        },
        body: WillPopScope(
          child: mainWidget(),
          onWillPop: () {
            if (_isDeviceNetworking == true) {
              // SilToast.show('设备正在配网中，请稍后...');
              SilToastExtends.toastMsg(
                  LangOf(context)?.deviceNetworking?.connectingDevicePrompt);
              return Future(() => false);
            }
            return Future(() => true);
          },
        ));
  }

  mainWidget() {
    return CustomScrollView(slivers: [
      /// 提示文字
      SliverToBoxAdapter(
        child: SilText(
          LangOf(context)?.deviceNetworking?.netStatePrompt, //'请保持网络
          color: ColorUtils.color_font_main,
          fontSize: 14.sp,
          maxLines: 0,
          textAlign: TextAlign.center,
          lineHeight: 1.5.w,
        ),
      ),

      //  链接进度
      SliverToBoxAdapter(
          child: Container(
        margin: EdgeInsets.only(left: 78.w, top: 100.w),
        alignment: Alignment.center,
        child: Column(
          children: [
            // 查找设备
            Obx(() => findDeviceView()),

            // 设备链接wifi
            Obx(() => connectWifiView()),

            // 设备链接到云（重启设备）
            Obx(() => connectICloudeView()),

            // 设备激活（检查是否配置成功）
            Obx(() => connectActiviteView())
          ],
        ),
      )),
    ]);
  }

  /// 查找设备
  findDeviceView() {
    return DNNetProgressItem(
      title: LangOf(context)?.deviceNetworking?.findDevice, //'查找设备',
      showProgressLine: false,
      prepareImgPath: 'imgs/device_net/device_net_pro_success.png',
      proState: _controller.proByFindDevice.value,
    );
  }

  /// 设备链接wifi
  connectWifiView() {
    return DNNetProgressItem(
      title:
          LangOf(context)?.deviceNetworking?.deviceConnectWifi, //'设备链接Wi-Fi',
      prepareImgPath: 'imgs/device_net/device_net_pro_wifi.png',
      failImgPath: 'imgs/device_net/device_net_pro_wifi_fail.png',
      proState: _controller.proByConnectState.value,
    );
  }

  /// 设备链接到云(设置家里的wifi)
  connectICloudeView() {
    return DNNetProgressItem(
      title: LangOf(context)?.deviceNetworking?.deviceConnectCloud, //'设备链接到云',
      prepareImgPath: 'imgs/device_net/device_net_pro_icloud.png',
      failImgPath: 'imgs/device_net/device_net_pro_icloud_fail.png',
      proState: _controller.proBySetWifi.value,
    );
  }

  /// 设备激活（重启设备）
  connectActiviteView() {
    return DNNetProgressItem(
      title: LangOf(context)?.deviceNetworking?.deviceActivation, //'设备激活',
      prepareImgPath: 'imgs/device_net/device_net_pro_activite.png',
      failImgPath: 'imgs/device_net/device_net_pro_activite_fail.png',
      proState: _controller.proByRestartDevice.value,
    );
  }
}
